package org.spring.springboot.app.agent.balancelog.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.adv.advcombhard.domain.po.AdvCombHardPO;
import org.spring.springboot.app.adv.advcombinfo.service.IAdvCombInfoService;
import org.spring.springboot.app.agent.balancelog.domain.po.SysOfficeBalanceLogPO;
import org.spring.springboot.app.agent.balancelog.domain.vo.SysOfficeBalanceLogSearchVO;
import org.spring.springboot.app.agent.balancelog.domain.vo.SysOfficeBalanceLogVO;
import org.spring.springboot.app.agent.balancelog.service.ISysOfficeBalanceLogService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Api(description = "代理商收支明细操作接口",tags = ApiIndex.SysOfficeBalanceLogController)
@RequestMapping(value = "/api/sysofficebalancelog")
@RestController
public class SysOfficeBalanceLogController {

    @Autowired
    private ISysOfficeBalanceLogService sysofficebalanceLogService;


    @ApiOperation(value = "查询代理商收支明细列表-分页")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<SysOfficeBalanceLogVO>>> findPageAll(
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "代理商ID", required = false) @RequestParam(defaultValue = "") String officeId,
            @ApiParam(value = "用户ID", required = false) @RequestParam(defaultValue = "") String userId,
            @ApiParam(value = "类型（多个）", required = false) @RequestParam(defaultValue = "") String types,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "开始创建时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束创建时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate
    ) {
        SysOfficeBalanceLogSearchVO searchvo=new SysOfficeBalanceLogSearchVO();  //查询对象
        if(StringUtils.isNotBlank(startCreateDate)){
            if(startCreateDate.indexOf(":")==-1){
                startCreateDate=startCreateDate+" 00:00:00";
            }
        }
        if(StringUtils.isNotBlank(endCreateDate)){
            if(endCreateDate.indexOf(":")==-1){
                endCreateDate=endCreateDate+" 23:59:59";
            }
        }
        searchvo.setOfficeId(officeId);
        searchvo.setUserId(userId);
        searchvo.setStartCreateDate(startCreateDate);
        searchvo.setEndCreateDate(endCreateDate);
        searchvo.setName(name);
        R<PageInfo<List<SysOfficeBalanceLogVO>>> result = new R<PageInfo<List<SysOfficeBalanceLogVO>>>();
        List<SysOfficeBalanceLogPO> _list = sysofficebalanceLogService.findPage(searchvo,pageNum, pageSize,types);
        PageInfo p =new PageInfo(_list);
        p.setList(getVOS(p.getList()));
        result.setData(p);
        return result;
    }

    private List<SysOfficeBalanceLogVO> getVOS(List<SysOfficeBalanceLogPO> _list) {
        List<SysOfficeBalanceLogVO> list = new ArrayList<SysOfficeBalanceLogVO>();
        if (_list != null && _list.size()>0) {
            for (SysOfficeBalanceLogPO po : _list) {
                SysOfficeBalanceLogVO vo = new SysOfficeBalanceLogVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }

    @ApiOperation(value = "根据Id获取单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<SysOfficeBalanceLogPO> find(
            @ApiParam(value = "代理商收支id", required = true) @PathVariable("id") String id
    ) {
        R<SysOfficeBalanceLogPO> result = new R<SysOfficeBalanceLogPO>();
        SysOfficeBalanceLogPO po = sysofficebalanceLogService.find(id);
        SysOfficeBalanceLogPO vo = new SysOfficeBalanceLogPO();
        BeanUtils.copyProperties(po,vo);
        result.setData(vo);
        return result;
    }




    @ApiOperation(value = "删除代理商收支信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "代理商收支id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        int code = sysofficebalanceLogService.delete(id);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("删除失败");
        }
        return result;
    }

    /**
     *  批量删除
     * @param ids
     */
    @ApiOperation(value="批量删除代理商收支信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public R deletes(@ApiParam(value = "代理商收支ids(用,分割)", required = true) @RequestParam("ids") String ids) {
        R result = new R();
        int code = sysofficebalanceLogService.deletes(ids);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("刪除失败");
        }
        return result;
    }

}
